﻿#define _CRT_SECURE_NO_WARNINGS 1
//计算布尔⼆叉树的值(easy):https://leetcode.cn/problems/evaluate-boolean-binary-tree/description/
class Solution {
public:
    bool evaluateTree(TreeNode* root) {
        if (!root->left)   return root->val;
        int x1 = evaluateTree(root->right);
        int x2 = evaluateTree(root->left);
        if (root->val == 2)  return x1 || x2;
        if (root->val == 3)  return x1 && x2;
        return 1;
    }
};

//求根节点到叶节点数字之和(medium):https://leetcode.cn/problems/sum-root-to-leaf-numbers/
class Solution {
public:
    int sumNumbers(TreeNode* root) {
        return dfs(root, 0);
    }

    int dfs(TreeNode* root, int prevsum)  //直接返回这条路径的值
    {
        int cursum = prevsum * 10 + root->val;
        if (!root->left && !root->right)   return cursum;
        int right = 0, left = 0;
        if (root->right)
        {
            right = dfs(root->right, cursum);
        }
        if (root->left)
        {
            left = dfs(root->left, cursum);
        }
        return right + left;
    }
};